package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

public class _114_FlattenBinaryTreeToLinkedList {
    public static void main(String[] args) {
        Solution solution = new _114_FlattenBinaryTreeToLinkedList().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {

        public void preOrder(List<TreeNode> list, TreeNode root) {
            if (root == null) {
                return;
            }
            list.add(root);
            preOrder(list, root.left);
            preOrder(list, root.right);
        }

        public void flatten(TreeNode root) {
            if (root == null) return;
            List<TreeNode> list = new ArrayList<>();
            preOrder(list, root);
            for (int i = 0; i < list.size() - 1; i++) {
                list.get(i).left = null;
                list.get(i).right = list.get(i + 1);
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}